import datetime
import multiprocessing
from util import *
import copy
from CloudSystem import run_once
import gc

def generate_default_args():
    args=Args()
    args.deploy_schedule_strategy="ours"
    args.request_num=10000
    args.out_dir="output_nocutlength"
    args.time_expand=0.1
    args.priority_queue=True
    args.spec_reliability=0.9999
    args.bare_metal_node_num=200
    args.elastic_deploy=False
    # args.part_percent_min=0
    # args.part_percent_max=0.99
    return args


def run_once_this(service_name,  no_cut_time_in_queue,  trace_id):
    print(f"start once {service_name}, {no_cut_time_in_queue}, {trace_id}")
    args=generate_default_args()
    
    args.aim_service_name=service_name
    args.no_cut_time_in_queue=no_cut_time_in_queue
    args.trace_id=trace_id
    args_copy=copy.deepcopy(args)
    run_once(args_copy)
    gc.collect()
    print(f"End once {service_name}, {no_cut_time_in_queue}, {trace_id}")
    
    


service_name=None
task_args_list=[]
for trace_id in [8]:#range(10):
    # for service_name in ["S_80535709","S_149012065","S_74554465","S_147601298","S_102676844","S_102676846","S_20083159","S_47614597","S_36241643","S_150593866","S_115273901","S_75752507"]:
        for no_cut_time_in_queue in [0]:#[0,10,100,1000,10000,100000,1000000,10000000]:
            task_args_list.append((service_name, no_cut_time_in_queue, trace_id))
        
                
with multiprocessing.Pool(processes=1) as pool:
        #   starmap 直接开始所有的
        #   apply_async 一个一个参数放入执行
        
        interval=0
        results=[]
        index=0
        while index<len(task_args_list):
            # length=min(batch_size, len(task_args_list)-index )
            result=pool.apply_async(run_once_this, task_args_list[index])
            results.append(result)
            if index!=0 and index%5==0:
                time.sleep(interval)
            index+=1
            
        for result in results:
            result.get()



print("End all sub threadings")
